<div class="clear_both"></div>
<div class="main_container">
<?php 
	
?>
<p class="text_uppercase native_f14 font_bold">Name:
<?php 
	if(isset($member)) echo $member->first_name ." ".$member->middle_name ." ". $member->last_name;
	
?><br>
Period: <?php echo DateUtils::formatDate($period->date_start, ConstantsUtils::$DATE_FRIENDLY_FORMAT). " - " .DateUtils::formatDate($period->date_end, ConstantsUtils::$DATE_FRIENDLY_FORMAT) ;?>
</p>
<?php if(isset($user_payroll)):?>
<?php echo form_open('payroll/update_data',array('id'=>'payroll_form'));?>
<table border="1" cellspacing="0" cellpadding="5" id="members" class="display">
<thead>
	<th>&nbsp;</th>
	<th>Hours</th>
	<th>Rate</th>
	<th width="100">Amount</th>
</thead>
<?php 
	$cnt = 2;
	$sub = 0;
	$field_count = 0;
	$total_contributions = 0;
	foreach($label as $lab) {
	?>
<tr class="timesheet_details">
	<td><?php echo $lab;?></td>
	<td width="65">
	<div class="edit_field" id="<?php echo $fields[$field_count];?>"><?php 
		switch($lab) {
			case 'Regular Hours':
				//$regular_hours = (($timesheet != null)?$timesheet->total_days:0) * ConstantsUtils::$EMPLOYEE_REGULAR_HOURS;
				if(isset($user_payroll)){
					$regular_hours = $user_payroll->regular_hours;
				}
				echo $regular_hours; 
				break;
			case 'Regular Overtime':
				//$overtime_hours = round(DateUtils::time_to_decimal($timesheet->total_approved_overtime)/60, 3);
				if(isset($user_payroll)){
					$overtime_hours = $user_payroll->regular_overtime;
				}
				echo $overtime_hours;
				break;
				
			case 'Reg. Night Diff':
				if(isset($user_payroll)){
					$night_differential = $user_payroll->regular_night_differential;
				}
				echo $night_differential;
				break;
				
			case 'Restday':
				if(isset($user_payroll)){
					$rest_day_hours = $user_payroll->restday;
				}
				echo $rest_day_hours;
				break;
				
			case 'Excess on Restday':
				if(isset($user_payroll)){
					$excess_rest_day_hours = $user_payroll->excess_restday;
				}
				echo $excess_rest_day_hours;
				break;
				
			case 'Restday Night Diff':
				if(isset($user_payroll)){
					$rest_day_night_differential = $user_payroll->restday_night_differential;
				}
				echo $rest_day_night_differential;
				break;
				
			case 'Special Holiday':
				if(isset($user_payroll)){
					$special_holiday = $user_payroll->special_holiday;
				}
				echo $special_holiday;
				break;
				
			case 'Excess on Special Holiday':
				if(isset($user_payroll)){
					$excess_special_holiday = $user_payroll->excess_special_holiday;
				}
				echo $excess_special_holiday;
				break;
				
			case 'Special Holiday Night Diff':
				if(isset($user_payroll)){
					$special_holiday_night_diff = $user_payroll->special_holiday_night_differential;
				}
				echo $special_holiday_night_diff;
				break;
				
			case 'Special Holiday on Restday':
				if(isset($user_payroll)){
					$special_holiday_restday = $user_payroll->special_holiday_restday;
				}
				echo $special_holiday_restday;
				break;
				
			case 'Excess Special Holiday on Restday':
				if(isset($user_payroll)){
					$excess_special_holiday_restday = $user_payroll->excess_special_holiday_restday;
				}
				echo $excess_special_holiday_restday;
				break;;
				
			case 'Legal Holiday':
				if(isset($user_payroll)){
					$legal_holiday = $user_payroll->legal_holiday;
				}
				echo $legal_holiday;
				break;
				
			case 'Excess on Legal Holiday':
				if(isset($user_payroll)){
					$excess_legal_holiday = $user_payroll->excess_legal_holiday;
				}
				echo $excess_legal_holiday;
				break;
				
			case 'Legal Holiday Unwork':
				if(isset($user_payroll)){
					$legal_holiday_unwork = $user_payroll->legal_holiday_unwork;
				}
				echo $legal_holiday_unwork;
				break;
				
			case 'Legal Holiday on Restday':
				if(isset($user_payroll)){
					$legal_holiday_restday = $user_payroll->legal_holiday_restday;
				}
				echo $legal_holiday_restday;
				break;
				
			case 'Excess Legal Holiday on Restday':
				if(isset($user_payroll)){
					$excess_legal_holiday_restday = $user_payroll->excess_legal_holiday_restday;
				}
				echo $excess_legal_holiday_restday;
				break;
				
			case 'Legal Holiday Night Diff':
				if(isset($user_payroll)){
					$legal_holiday_night_diff = $user_payroll->legal_holiday_night_differential;
				}
				echo $legal_holiday_night_diff;
				break;
				
			case 'Adjustment/CA':
				if(isset($user_payroll)){
					$adjustment = $user_payroll->adjustment;
				}
				echo $adjustment;
				break;
				
			case 'Incentives/Maternity/Sickness Reimbursement':
				if(isset($user_payroll)){
					$incentives = $user_payroll->incentives;
				}
				echo $incentives;
				break;
				
			case 'Undertime':
				if(isset($user_payroll)){
					$undertime = $user_payroll->approved_undertime;
				}
				echo $undertime;
				break;
				
			case 'Late':
				if(isset($user_payroll)){
					$late = $user_payroll->approved_late;
				}
				echo $late;
				break;
			default:
				$default_value = 0.00;
				echo '0.00';
				break;
		}
		$field_count++;
	?></div>
	</td>
	<td>
	
	<?php 
	if(isset($rates[$cnt])) {
		echo $rates[$cnt];
	} else if($cnt == 21 || $cnt == 22) {
		echo $rates[2];
	} else {
		echo 0;
	}
	?></td>
	<td>
		
	<?php 
		switch($cnt){
				
			case 3:
				$rate = $overtime_hours * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "regular_overtime";
				echo round($rate,3);
				break;
			case 2:

				$rate = $regular_hours * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "regular_hours";
				echo round($rate,3);
				break;
				
			case 4:
				$rate = $night_differential * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "regular_night_differential";
				echo round($rate,3);
				break;
				
			case 5:
				$rate = $rest_day_hours * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "restday";
				echo round($rate,3);
				break;
				
			case 6:
				$rate = $excess_rest_day_hours * $rates[$cnt];
				$sub += $rate;
				echo round($rate,3);
				$hidden_field = "excess_restday";
				break;
				
			case 7:
				$rate = $rest_day_night_differential * $rates[$cnt];
				$sub += $rate;
				echo round($rate,3);
				$hidden_field = "restday_night_differential";
				break;
				
			case 8:
				$rate = $special_holiday * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "special_holiday";
				echo round($rate,3);
				break;
				
			case 9:
				$rate = $excess_special_holiday * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "excess_special_holiday";
				echo round($rate,3);
				break;
				
			case 10:
				$rate = $special_holiday_night_diff * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "special_holiday_night_differential";
				echo round($rate,3);
				break;
				
			case 11:
				$rate = $special_holiday_restday * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "special_holiday_restday";
				echo round($rate,3);
				break;
				
			case 12:
				$rate = $excess_special_holiday_restday * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "excess_special_holiday_restday";
				echo round($rate,3);
				break;
				
			case 13:
				$rate = $legal_holiday * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "legal_holiday";
				echo round($rate,3);
				break;
				
			case 14:
				$rate = $excess_legal_holiday * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "excess_legal_holiday";
				echo round($rate,3);
				break;
				
			case 15:
				$rate = $legal_holiday_unwork * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "legal_holiday_unwork";
				echo round($rate,3);
				break;
				
			case 16:
				$rate = $legal_holiday_restday * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "legal_holiday_restday";
				echo round($rate,3);
				break;
				
			case 17:
				$rate = $excess_legal_holiday_restday * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "excess_legal_holiday_restday";
				echo round($rate,3);
				break;
				
			case 18:
				$rate = $legal_holiday_night_diff * $rates[$cnt];
				$sub += $rate;
				$hidden_field = "legal_holiday_night_differential";
				echo round($rate,3);
				break;
				
			case 19:
				$rate = $adjustment * $rates[$cnt];
				$sub += $rate;
				echo round($rate,3);
				$hidden_field = "adjustment";
				break;
				
			case 20:
				$rate = $incentives * $rates[$cnt];
				$sub += $rate;
				echo round($rate,3);
				$hidden_field = "incentives";
				break;
				
			case 21:
				$rate = -$undertime * $rates[2];
				$sub += $rate;
				echo round($rate,3);
				$hidden_field = "approved_undertime";
				break;
			
			case 22:
				$rate = -$late * $rates[2];
				$sub += $rate;
				echo round($rate,3);
				$hidden_field = "approved_late";
				break;
			default:
				echo $default_value * $rates[2];
				break;
		}
		
		//$total_contributions = isset($contributions->total_deductions)?$contributions->total_deductions:0.00;
	?>
	
	<input type="hidden" name="<?php echo $hidden_field?>" value="<?php echo round($rate,3);?>">
	</td>
</tr>
<?php 
	$cnt++;
	}?>
	
<tr class="timesheet_details">
	<td><span class="text_uppercase font_bold red_text">Earnings (PHP)</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>
	<span class="font_bold red_text"><?php echo CommonUtils::number_format($sub, 2)?></span>
	<input type="hidden" name="gross_salary" value="<?php echo CommonUtils::number_format($sub, 2)?>">
	</td>
</tr>
<tr class="timesheet_details">
	<td colspan="4"><span class="text_uppercase font_bold red_text">Deductions</span></td>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase">Witholding Tax (PHP)</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td><div class="edit_field"><?php echo isset($contributions)?$contributions->tax->employee:"0.00";?></div>
	<input type="hidden" name="tax" value="<?php echo isset($contributions)?$contributions->tax->employee:"0.00";?>" />
	</td>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase">SSS (PHP)</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<?php 
		if($user_payroll->sss < 1) {
			if(isset($contributions->sss->employee)){
				$sss = $contributions->sss->employee;
			} else {
				$sss = 0.00;
			}
		} else {
			$sss = $user_payroll->sss;
		}
		$total_contributions+=$sss;
	?>
	<td><div class="edit_field" id="sss"><?php echo $sss;?></div>
	<input type="hidden" name="sss" value="<?php echo $sss;?>" />
	</td>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase">HDMF (Pag-ibig) (PHP)</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<?php 
		if($user_payroll->hdmf < 1) {
			if(isset($contributions->hdmf->employee)){
				$hdmf = $contributions->hdmf->employee;
			} else {
				$hdmf = 0.00;
			}
		} else {
			$hdmf = $user_payroll->hdmf;
		}
		$total_contributions+=$hdmf;
	?>
	<td>
		<div class="edit_field" id="hdmf"><?php echo $hdmf;?></div>
		<input type="hidden" name="hdmf" value="<?php echo $hdmf;?>" />
	</td>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase">PhilHealth (PHP)</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<?php 
		if($user_payroll->philhealth < 1) {
			if(isset($contributions->philhealth->employee)){
				$philhealth = $contributions->philhealth->employee;
			} else {
				$philhealth = 0.00;
			}
		} else {
			$philhealth = $user_payroll->philhealth;
		}
		$total_contributions+=$philhealth;
	?>
	<td><div class="edit_field" id="philhealth"><?php echo $philhealth;?></div>
	<input type="hidden" name="philhealth" value="<?php echo $philhealth;?>" />
	</td>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase">SSS Loan (PHP)</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td><div class="edit_field" id="sss_loan"><?php echo ($user_payroll->sss_loan < 1)?"0.00": $user_payroll->sss_loan;?></div>
	<input type="hidden" name="sss_loan" value="<?php echo ($user_payroll->sss_loan < 1)?"0.00": $user_payroll->sss_loan;?>">
	</td>
	<?php $total_contributions += $user_payroll->sss_loan; ?>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase">HDMF Loan (PHP)</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td><div class="edit_field" id="hdmf_loan"><?php echo ($user_payroll->hdmf_loan < 1)?0.00:$user_payroll->hdmf_loan;?></div>
	<input type="hidden" name="hdmf_loan" value="<?php echo ($user_payroll->hdmf_loan < 1)?0.00:$user_payroll->hdmf_loan;?>">
	</td>
	<?php $total_contributions += $user_payroll->hdmf_loan; ?>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase">Others (PHP)</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase">&nbsp; &raquo; Canteen 1</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td><div class="edit_field" id="canteen"><?php echo isset($user_payroll->canteen)?$user_payroll->canteen:0.00;?></div>
	<input type="hidden" name="canteen" value="<?php echo isset($user_payroll->canteen)?$user_payroll->canteen:0.00;?>">
	</td>
	<?php $total_contributions += $user_payroll->canteen; ?>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase">&nbsp; &raquo; Canteen 2</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td><div class="edit_field" id="canteen2"><?php echo isset($user_payroll->canteen2)?$user_payroll->canteen2:0.00;?></div>
	<input type="hidden" name="canteen2" value="<?php echo isset($user_payroll->canteen2)?$user_payroll->canteen2:0.00;?>">
	</td>
	<?php $total_contributions += $user_payroll->canteen2; ?>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase">&nbsp; &raquo; Uniform/ID</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td><div class="edit_field" id="uniform"><?php echo isset($user_payroll->uniform)?$user_payroll->uniform:0.00;?></div>
	<input type="hidden" name="uniform" value="<?php echo isset($user_payroll->uniform)?$user_payroll->uniform:0.00;?>">
	</td>
	<?php $total_contributions += $user_payroll->uniform; ?>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase">&nbsp; &raquo; Others</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td><div class="edit_field" id="other_deductions"><?php echo isset($user_payroll->other_deductions)?$user_payroll->other_deductions:0.00;?></div>
	<input type="hidden" name="other_deductions" value="<?php echo isset($user_payroll->other_deductions)?$user_payroll->other_deductions:0.00;?>">
	</td>
	<?php $total_contributions += $user_payroll->other_deductions; ?>
</tr>
<tr class="timesheet_details">
	<td><span class="text_uppercase font_bold red_text">Total Deductions (PHP)</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td><div class="font_bold red_text"><?php echo CommonUtils::number_format($total_contributions,2);?></div></td>
</tr>
<tr class="summary">
	<td><span class="text_uppercase font_bold ">Total Earnings/Net Homepay  (PHP)</span></td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
	<td><span class="font_bold text_orange font16"><?php echo CommonUtils::number_format($sub - $total_contributions, 2)?></span>
	<?php echo form_hidden("net_pay", CommonUtils::number_format($sub - $total_contributions, 2));?>
	</td>
</tr>
<tr class="timesheet_details">
	<td colspan="4" align="right">
	<?php echo form_hidden("status",'confirmed');?>
	<?php echo form_hidden("pid",$user_payroll->id);?>
	<?php echo form_hidden("user_id",$user_payroll->user_id);?>
	<?php echo form_hidden("payroll_period_id",$user_payroll->payroll_period_id);?>
	<input type="button" id="back_button" value="&laquo; Back">
	<?php if($user_payroll->status != 'submitted'):?>
	<?php if($user_payroll->status != 'confirmed'):?>
	<input type="submit" class="submit_button" value="Update" status="confirmed"> 
	<?php endif;?>
	<input type="submit" class="submit_button" value="Reviewed and Submit" status="submitted">
	<?php endif;?>
	</td>
</tr>
</table>
</form>
<?php endif;?>
</div>

<script language="javascript">
$(document).ready(function() {
	$("#members_filter label").hide();
	$("#members_info").hide();

	<?php if($user_payroll->status != 'submitted'):?>
	$('.edit_field').editable("<?php echo base_url()?>payroll/update_field/<?php echo $user_payroll->payroll_period_id?>/<?php echo $member->user_id?>/<?php echo $user_payroll->id?>",
			{indicator : 'Saving...',
			callback: function(val){
				location.reload();
			}
			});
	<?php endif;?>

	
	$('input.submit_button').click(function(){

		$('input[name="status"]').val($(this).attr('status')); 
		//$('#payroll_form').submit();
		
	});

	$('input#back_button').click(function(){
		document.location = "<?php echo base_url()?>payroll/index/<?php echo $user_payroll->payroll_period_id?>";
	})
	
	
} );
</script>
<?php 
	//$time2 = CommonUtils::getCurrentTime();
	//$diff = $time2 - $start_time;
	//echo "processed: " . $diff ." seconds";
?>